package com.example.myapplication.government;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class NewRquestPageActivity extends AppCompatActivity {

    private String demandApterID;
    private EditText editTextTitle;
    private EditText editTextContent;
    private EditText editTextDepartment;
    private EditText editTextPhone;
    private Button buttonSubmit;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private Gson gson;
    private Handler handler;
    private OkHttpClient okHttpClient;
    private Request  request;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_rquest_page);
        Intent intent = getIntent();
        demandApterID = intent.getStringExtra("DemandApterID");
        initview();
        //点击按键发表诉求
        butNewRquestPage();
    }

    private void butNewRquestPage() {
        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String editTextTitlestring = editTextTitle.getText().toString();
                String editTextContentstring = editTextContent.getText().toString();
                String editTextDepartmentstring = editTextDepartment.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("title",editTextTitlestring);
                    jsonObject.put("appealCategoryId",demandApterID);
                    jsonObject.put("content",editTextContentstring);
                    jsonObject.put("undertaker",editTextDepartmentstring);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                RequestBody requestBody =  RequestBody.create(jsonObject.toString(),JSON);
                request  = new Request.Builder()
                        .addHeader("token", TokenBean.token)
                        .url("http://124.93.196.45:10001/prod-api/api/gov-service-hotline/appeal")
                        .post(requestBody)
                        .build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(NewRquestPageActivity.this,"发表成功",Toast.LENGTH_SHORT);
                            }
                        });
                    }
                });
            }
        });
    }

    private void initview() {
        editTextTitle = findViewById(R.id.editTextTitle);
        editTextContent = findViewById(R.id.editTextContent);
        editTextDepartment = findViewById(R.id.editTextDepartment);
        buttonSubmit = findViewById(R.id.buttonSubmit);

        gson = new Gson();
        handler = new Handler();
        okHttpClient = new OkHttpClient();
    }
}